package hos.delegate;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import androidx.activity.ComponentActivity;
import androidx.activity.result.ActivityResultCaller;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.DefaultLifecycleObserver;
import androidx.lifecycle.LifecycleOwner;

import java.util.UUID;

/**
 * <p>Title: FileDelegate </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2024-03-13 13:33
 */
public abstract class BaseDelegate implements DefaultLifecycleObserver {

    @NonNull
    private String mWho = UUID.randomUUID().toString();

    @Nullable
    private LifecycleOwner lifecycleOwner;

    protected BaseDelegate(@NonNull LifecycleOwner lifecycleOwner) {
        this.lifecycleOwner = lifecycleOwner;
        lifecycleOwner.getLifecycle().addObserver(this);
    }

    @NonNull
    public String getWho() {
        return mWho;
    }

    public Bundle getBundle() {
        if (lifecycleOwner instanceof Fragment) {
            return ((Fragment) lifecycleOwner).getArguments();
        }
        if (lifecycleOwner instanceof Activity) {
            Activity activity = (Activity) lifecycleOwner;
            Intent intent = activity.getIntent();
            if (intent == null) {
                return null;
            }
            return intent.getExtras();
        }
        return null;
    }

    @NonNull
    protected ComponentActivity requireActivity() {
        if (lifecycleOwner instanceof ComponentActivity) {
            return (ComponentActivity) lifecycleOwner;
        }
        if (lifecycleOwner instanceof Fragment) {
            return ((Fragment) lifecycleOwner).requireActivity();
        }
        throw new IllegalStateException("PermissionDelegate " + this + " null an activity.");
    }

    @NonNull
    protected ActivityResultCaller requireActivityResultCaller() {
        if (lifecycleOwner instanceof ActivityResultCaller) {
            return (ActivityResultCaller) lifecycleOwner;
        }
        throw new IllegalStateException("PermissionDelegate " + this + " null an ActivityResultCaller.");
    }


    /**
     * Get the FragmentManager if it's in Activity, or the ChildFragmentManager if it's in Fragment.
     *
     * @return The FragmentManager to operate Fragment.
     */
    @NonNull
    protected FragmentManager fragmentManager() {
        if (lifecycleOwner instanceof Fragment) {
            return ((Fragment) lifecycleOwner).getChildFragmentManager();
        }
        if (lifecycleOwner instanceof FragmentActivity) {
            return ((FragmentActivity) lifecycleOwner).getSupportFragmentManager();
        }
        throw new IllegalStateException("PermissionDelegate " + this + " null an FragmentActivity.");
    }

    protected void runMain(Runnable run) {
        requireActivity().runOnUiThread(run);
    }

    @Override
    public void onDestroy(@NonNull LifecycleOwner owner) {
        owner.getLifecycle().removeObserver(this);
        lifecycleOwner = null;
    }

    public void close() {
        if (lifecycleOwner instanceof Activity) {
            ((Activity) lifecycleOwner).finish();
        } else if (lifecycleOwner instanceof androidx.fragment.app.Fragment) {
            FragmentActivity activity = ((Fragment) lifecycleOwner).getActivity();
            if (activity != null) {
                activity.finish();
            }
        }
    }
}
